package io.renren.modules.xsl.service.impl;

import com.alibaba.fastjson.JSONObject;
import io.renren.modules.oatools.utils.http.HttpRequestSend;
import io.renren.modules.xsl.service.XslOrgService;
import io.renren.modules.xsl.utils.AesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import static io.renren.modules.oatools.utils.http.OAHttpRequestUtils.requestSend;

@Service
public class XslOrgServiceImpl implements XslOrgService {
    private final static Logger logger = LoggerFactory.getLogger(XslOrgServiceImpl.class);

    /**
     * 外网地址
     */
    @Value("${xslSuite.out-target-url}")  // 对应 yml 中的 myapp.name
    private String outTargetUrl;

    /**
     * 集团下所有单位
     *
     * @return
     */
    @Override
    public Map<String, Object> getGroupFindCompanyList() {
        String userId = AesUtil.encode("16673236249", this.getDevToken());

        HttpRequestSend httpRequestSend = new HttpRequestSend();
        httpRequestSend.setRequestUrl(outTargetUrl + "/open-api/group/findCompanyList?user_id=16673236249");
        httpRequestSend.setRequestType("GET");
        httpRequestSend.setRequestHeaders("{devToken:\"" + this.getDevToken() + "\"}");

        Map<String, Object> res = null;
        try {
            res = requestSend(null, httpRequestSend);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return res;
    }

    /**
     * 获取所有部门
     *
     * @return
     */
    @Override
    public Map<String, Object> getDepartmentFindAll() throws IOException {
        //获取部门信息
        HttpRequestSend httpRequestSend = new HttpRequestSend();
        httpRequestSend.setRequestUrl(outTargetUrl + "/open-api/department/findAll");
        httpRequestSend.setRequestType("GET");
        httpRequestSend.setRequestHeaders("{devToken:\"" + this.getDevToken() + "\"}");
        Map<String, Object> res = requestSend(null, httpRequestSend);
        return res;
    }

    /**
     * 获取所有职位
     *
     * @return
     */
    @Override
    public Map<String, Object> getJobFindAll() throws IOException {
        String devToken = this.getDevToken();
        HttpRequestSend httpRequestSendJob = new HttpRequestSend();
        httpRequestSendJob.setRequestUrl(outTargetUrl + "/open-api/job/findAll");
        httpRequestSendJob.setRequestType("GET");
        httpRequestSendJob.setRequestHeaders("{devToken:\"" + devToken + "\"}");
        Map<String, Object> resJob = requestSend(null, httpRequestSendJob);
        return resJob;
    }

    /**
     * 部门查询人员
     *
     * @return
     */
    @Override
    public Map<String, Object> getFindDeptPersons() throws IOException {
        String devToken = this.getDevToken();
        HttpRequestSend httpRequestSendDeptPersons = new HttpRequestSend();
        httpRequestSendDeptPersons.setRequestUrl(outTargetUrl + "/open-api/person/findDeptPersons?deptId=6404764104082829708");
        httpRequestSendDeptPersons.setRequestType("GET");
        httpRequestSendDeptPersons.setRequestHeaders("{devToken:\"" + devToken + "\"}");
        Map<String, Object> resDeptPersons = requestSend(null, httpRequestSendDeptPersons);
        return resDeptPersons;
    }


    /**
     * 获取token
     *
     * @return
     */
    public String getDevToken() {
        HttpRequestSend httpRequestToke = new HttpRequestSend();
        httpRequestToke.setRequestUrl(outTargetUrl + "/open-api/token/getDevToken");
        httpRequestToke.setRequestType("POST");
        httpRequestToke.setRequestBody("{\"apiKey\":\"b2cedf7359aa2a4d\"}");
        try {
            Map<String, Object> resDeptPersons = requestSend(null, httpRequestToke);
            JSONObject jsonObject = JSONObject.parseObject(resDeptPersons.get("mag").toString());
            JSONObject data = jsonObject.getJSONObject("data");
            return data.getString("devToken");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            logger.info("获取薪事力token");
        }
    }

}